using System;
using System.Windows;
using Microsoft.Practices.ObjectBuilder;
using SCSF=SCSFContrib.CompositeUI.WPF.Workspaces;
using SCSFContrib.CompositeUI.WPF.Workspaces;

namespace TabWorkspace
{
	public partial class MainWindow : IBuilderAware
	{
		private readonly RootWorkItem _workItem;
		private SmartPart1 _smartPart1;
		private SmartPart2 _smartPart2;
		private SmartPart3 _smartPart3;

		public MainWindow([Dependency] RootWorkItem workItem)
		{
			InitializeComponent();
			_workItem = workItem;
		}

		public void OnBuiltUp(string id)
		{
			_smartPart1 = _workItem.SmartParts.AddNew<SmartPart1>();
			_smartPart2 = _workItem.SmartParts.AddNew<SmartPart2>();
			_smartPart3 = _workItem.SmartParts.AddNew<SmartPart3>();
		}

		public void OnTearingDown()
		{
		}

		private void _show_Click(object sender, EventArgs e)
		{
			object smartPart = GetSmartPart(sender);
			TabSmartPartInfo smartPartInfo = new TabSmartPartInfo("Title for Smart Part " + (sender as FrameworkContentElement).Tag);
			_tabWorkspace.Show(smartPart, smartPartInfo);
		}

		private void _hide_Click(object sender, EventArgs e)
		{
			object smartPart = GetSmartPart(sender);

			if (_tabWorkspace.SmartParts.Contains(smartPart))
			{
				_tabWorkspace.Hide(smartPart);
			}
		}

		private void _close_Click(object sender, EventArgs e)
		{
			object smartPart = GetSmartPart(sender);

			if (_tabWorkspace.SmartParts.Contains(smartPart))
			{
				_tabWorkspace.Close(smartPart);
			}
		}

		private object GetSmartPart(object sender)
		{
			string spStr = (sender as FrameworkContentElement).Tag as string;

			if (spStr == "1")
			{
				return _smartPart1;
			}
			else if (spStr == "2")
			{
				return _smartPart2;
			}
			else
			{
				return _smartPart3;
			}
		}
	}
}